package com.thoughtworks.bytecode2class.attribute;

import com.thoughtworks.bytecode2class.ClassReader;

import java.util.ArrayList;
import java.util.List;

public class ExceptionsAttribute extends Attribute {

    private int exceptionCount;
    private List<String> exceptions;

    public ExceptionsAttribute(ClassReader classReader, String name) {
        super(classReader, name);
    }

    protected void parseAttributeContent() {
        parseExceptionCount();
        parseExceptions();
    }

    private void parseExceptions() {
        for (int i = 0; i < exceptionCount; i++) {
            int index = classReader.forwardChar();
            String exceptionName = classReader.getConstantPool().getClassConstant(index).getName();
            exceptions.add(exceptionName);
        }
    }

    private void parseExceptionCount() {
        exceptionCount = classReader.forwardChar();
        exceptions = new ArrayList<String>(exceptionCount);
    }

    public List<String> getExceptions() {
        return exceptions;
    }

}
